using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour
{
    public Transform[] spawnPoint;
    public SpawnData[] spawnDatas;
    private float timer;
    public int level;
    public float levelTime;

    private void Awake()
    {
        levelTime = GameManager.instance.maxGameTime / spawnDatas.Length;
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (GameManager.instance.isLive)
        {
            timer += Time.deltaTime;
            level = Mathf.Min(Mathf.FloorToInt(GameManager.instance.gameTime / levelTime),spawnDatas.Length - 1);
            if(level == 0)
            {
                level++;
            }
            if (timer > spawnDatas[level - 1].spawnTime)
            {
                timer = 0;
                Spawn();
            }
        }
        
    }

    private void Spawn()
    {
        int enemyLevel = Random.Range(0, level);
        GameObject enemy = GameManager.instance.enemypool.Get(enemyLevel);
        enemy.transform.position = spawnPoint[Random.Range(0, spawnPoint.Length)].position;
        enemy.GetComponent<Enemy>().Init(spawnDatas[enemyLevel]);
    }
}

[System.Serializable]
public class SpawnData
{
    public int spriteType;
    public float spawnTime;
    public int health;
    public float speed;
}
